package www.csdn.net.day56.servlet;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Decoder.BASE64Encoder;
import www.csdn.net.day56.bean.Admin;
import www.csdn.net.day56.service.AdminService;
import www.csdn.net.day56.service.impl.AdminServiceImpl;

public class AdminServlet extends HttpServlet {

	private AdminService adminService = new AdminServiceImpl();

	private long expires = 24 * 60 * 60; // 1天

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String method = request.getMethod();
		System.out.println("请求的方法::::" + method);
		// 获取请求参数中 操作的标识符
		String oper = request.getParameter("oper");

		if ("login".equals(oper)) {
			// 处理登陆
			// 获取用户名和密码
			String name = request.getParameter("name");
			String pass = request.getParameter("pass");
			// 获取标记 是否自动登录标识符
			String mark = request.getParameter("mark");

			// 验证用户名和密码是否正确
			Admin entity = adminService.checkLogin(name, pass);

			if (entity != null) {
				// 判断是自动登录处理
				if ("mark".equals(mark)) {
					// 获取默认记住的天数
					String day = request.getParameter("day");
					// 转换成有效的时间
					expires = Integer.valueOf(day) * expires;
					// 声明cookie
					Cookie autoCookie = null;
					// 获取所有cookie
					Cookie cookies[] = request.getCookies();
					// 遍历cookie
					for (Cookie cookie : cookies) {
						// 判断是否存在自动登陆记录
						if ("autologin".equals(cookie.getName())) {
							autoCookie = cookie; // 赋值

							// 当cookie存在的时候,我需要重新设置值
							long time = (System.currentTimeMillis() + expires * 1000);
							// cookie拼接的value值,(可以根据自己的想法设计)
							String newValue = name + ":" + time + ":" + md5Value(name + ":" + pass + ":" + time);
							// 设置值
							autoCookie.setValue(newValue);
						} else {
							// 不存在创建
							// name+":"+time+":"+md5(name:pass:time)
							long time = System.currentTimeMillis() + expires * 1000;
							// cookie拼接的value值,(可以根据自己的想法设计)
							String cookieValue = name + ":" + time + ":" + md5Value(name + ":" + pass + ":" + time);
							// 创建cookie
							autoCookie = new Cookie("autologin", cookieValue);
						}
					}

					autoCookie.setMaxAge((int) expires);
					autoCookie.setPath("/day56");
					// 添加cookie
					response.addCookie(autoCookie);

				}
				// admin存入到session
				request.getSession().setAttribute("admin", entity);
				// 成功登陆后的操作
				request.getRequestDispatcher("./jsp/007_layout.jsp").forward(request, response);

			} else {
				request.setAttribute("msg", "用户名或者密码错误");
				request.getRequestDispatcher("./index.jsp").forward(request, response);
			}

		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * md5加密处理
	 * 
	 * @param value
	 * @return
	 */
	public String md5Value(String value) {
		try {
			MessageDigest digest = MessageDigest.getInstance("md5");
			byte result[] = digest.digest(value.getBytes());
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encode(result);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

}